//
//  OnweDetailController.m
//  PropertyMananger
//
//  Created by Mansion on 16/12/31.
//  Copyright © 2016年 BH. All rights reserved.
//

#import "OnweDetailController.h"
#import "OnwerDetailCell.h"
#import "DetailTableHeaderView.h"

@interface OnweDetailController ()

@property (nonatomic,strong) DetailTableHeaderView * headerView;

//标题
@property (nonatomic,strong) NSArray * ownerTitleArray;
@property (nonatomic,strong) NSArray * personTitleArr;

/**业主信息数据源**/
@property (nonatomic,strong) NSArray * ownerInfoArray;
/**个人信息数据源**/
@property (nonatomic,strong) NSArray * personInfoArray;
/*组头数据源*/
@property (nonatomic,strong) NSArray * headerArray;

@end

@implementation OnweDetailController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self initTitleViewName:@"业主信息详情"];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initTableViewWithView:self.view frame:CGRectMake(0, 0, BHscreenW, BHscreenH - 64) isGroup:NO];
    self.headerView = [[DetailTableHeaderView alloc]initWithFrame:CGRectMake(0, 0, BHscreenW, 150)];
    self.tableView.tableHeaderView = self.headerView;
    self.headerView.nickNameLabel.text = self.model.OwnerName;
    self.headerView.sexLabel.text = self.model.Sex;
    self.headerView.phoneLabel.text = self.model.LinkTel;
    
    [self.headerView.phoneButton addTarget:self action:@selector(clickPhone:) forControlEvents:UIControlEventTouchUpInside];
    
    self.ownerTitleArray = @[@"小      区:",@"楼      栋:",@"单      元:",@"房      号:"];
    
    //  DLog(@"传来的=== %ld",self.ownerInfoArray.count);
    
    self.headerArray = [NSMutableArray arrayWithObjects:self.model.Withhold, nil];
    
}

#pragma mark -代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.model.roomInfoArray.count == 0) {
        
        return 0;
    }else{
    return  self.model.roomInfoArray.count;
    }
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.model.roomInfoArray.count == 0) {
        
        return 0;
    }else{
     return self.ownerTitleArray.count;
    }
   
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.model.roomInfoArray.count == 0) {
        
        return nil;
    }else{
    
        OnwerDetailCell * cell = [OnwerDetailCell cellWithTabelView:tableView];
        
        NSArray * array = self.model.roomInfoArray;
        
        OwnerRoomInfoData * roomModle = array[indexPath.section];
        DLog(@"社区名称 === %@",roomModle.Buildname);
        
        if (indexPath.row == 0) {

                cell.infoLabel.text = userModel.CommunityName;
            
        }else if (indexPath.row == 1){
            if (roomModle.Buildname == nil) {
                
                cell.infoLabel.text = @"暂无";
            }else{
                cell.infoLabel.text = roomModle.Buildname;
            }
            
        }else if (indexPath.row == 2){
            if (roomModle.Unitname == nil) {
                
                cell.infoLabel.text = @"暂无";
            }else{
                cell.infoLabel.text = roomModle.Unitname;
            }
            
        }else if (indexPath.row == 3){
            if (roomModle.RoomName == nil) {
                
                cell.infoLabel.text = @"暂无";
            }else{
                cell.infoLabel.text = roomModle.RoomName;
            }
            
        }
        
        cell.titleLabel.text = self.ownerTitleArray[indexPath.row];
        
     return cell;
    }
    
    return nil;
    
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (self.headerArray.count == 0) {
        
        return 0;
        
    }else{
    
        static NSString * headerID = @"headerID";
        
        UITableViewHeaderFooterView * headerView = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:headerID];
        UILabel * headerLabel = [[UILabel alloc]init];
        [headerView addSubview:headerLabel];
        headerLabel.text = [NSString stringWithFormat:@"房屋: %ld",section+1];
        headerLabel.font = BHFont(15);
        headerLabel.textColor = BHColor(51, 51, 51);
        
        [headerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(headerView).with.offset(15);
            make.centerY.equalTo(headerView).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(50, 25));
        }];
        
        [headerView addSubview:headerLabel];
        return headerView;

    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    if (self.model.roomInfoArray.count == 0) {
        
        return 0;
    }else{
       return 45;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.model.roomInfoArray.count == 0) {
        
        return 0;
    }else{
    return 50;
    }
    
}
#pragma mark - 打电话点击事件
- (void)clickPhone:(UIButton *)button
{
    NSLog(@"打电话");
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",self.model.LinkTel];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
    
}
@end
